package com.fzn3.day03;

import java.util.Iterator;

/**
 * @author fzn
 * 描述：  双向链表-带头节点
 * @program DataStructure
 * @create 2023/3/9 12:42
 **/
public class DoubleLinkedListSentinel implements Iterable<Integer> {
    public static class Node {
        Node prev;
        int value;
        Node next;

        public Node(Node prev, int value, Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }

    /**
     * 头
     */
    private Node head;

    /**
     * 尾指针
     */
    private Node tail;

    public DoubleLinkedListSentinel() {
        head = new Node(null, 666, null);
        tail = new Node(null, 888, null);
        head.next = tail;
        tail.next = head;
    }

    /**
     * 找到节点
     *
     * @param index 下标
     * @return {@link Node}
     */
    private Node findNode(int index) {
        int i = -1;
        Node p = head;
        while (p != tail) {
            if (i == index) {
                return p;
            }
            p = p.next;
            i++;
        }
        return null;
    }

    /**
     * 插入
     *
     * @param index 下标
     * @param value 值
     */
    public void insert(int index, int value) {
        // 上一个节点
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw illegalIndex(index);
        }
        Node next = prev.next;
        Node newNode = new Node(prev, value, next);
        prev.next = newNode;
        newNode.prev = newNode;
    }

    /**
     * @param index 下标
     */
    public void remove(int index) {
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw illegalIndex(index);
        }
        Node removed = prev.next;
        if (removed == null) {
            throw illegalIndex(index);
        }
        Node next = removed.next;
        prev.next = next;
        next.prev = prev;
    }

    public void removeFirst(int index) {
        remove(0);
    }

    public void removeLast() {
        Node removed = tail.prev;
        if (removed == head) {
            throw illegalIndex(0);
        }
        Node prev = removed.prev;
        prev.next = tail;
        tail.prev = prev;
    }

    /**
     * 在最后添加
     *
     * @param value 值
     */
    public void addLast(int value) {
        Node last = tail.prev;
        Node added = new Node(last, value, tail);
        last.next = added;
        tail.prev = added;
    }


    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p = head.next;

            @Override
            public boolean hasNext() {
                return p != tail;
            }

            @Override
            public Integer next() {
                int value = p.value;
                p = p.next;
                return value;
            }
        };
    }

    private static IllegalArgumentException illegalIndex(int index) {
        return new IllegalArgumentException(String.format("index[%d] 不合法", index));
    }
}
